package com.qijia.common.dao.impl;

import java.util.List;

import org.springframework.context.ApplicationContext;
import com.qijia.common.dao.ResourceDAO;
import com.qijia.common.domain.Resource;

/**
 * A data access object (DAO) providing persistence and search support for
 * CommonResource entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.qijia.common.domain.Resource
 * @author MyEclipse Persistence Tools
 */
public class ResourceDAOImpl extends JpaDaoSupportImpl<Resource> implements
		ResourceDAO {
	// property constants
	public static final String RESOURCE_NAME = "ResourceName";
	public static final String RESOURCE_TITLE = "ResourceTitle";
	public static final String RESOURCE_CODE = "ResourceCode";
	public static final String RESOURCE_URL = "ResourceUrl";
	public static final String RESOURCE_TYPE = "ResourceType";
	
	public List<Resource> findByCResourceName(Object CResourceName,
			int... rowStartIdxAndCount) {
		return findByProperty(RESOURCE_NAME, CResourceName,
				rowStartIdxAndCount);
	}

	public List<Resource> findByCResourceTitle(Object CResourceTitle,
			int... rowStartIdxAndCount) {
		return findByProperty(RESOURCE_TITLE, CResourceTitle,
				rowStartIdxAndCount);
	}

	public List<Resource> findByCResourceCode(Object CResourceCode,
			int... rowStartIdxAndCount) {
		return findByProperty(RESOURCE_CODE, CResourceCode,
				rowStartIdxAndCount);
	}

	public List<Resource> findByCResourceUrl(Object CResourceUrl,
			int... rowStartIdxAndCount) {
		return findByProperty(RESOURCE_URL, CResourceUrl, rowStartIdxAndCount);
	}

	public List<Resource> findByCResourceType(Object CResourceType,
			int... rowStartIdxAndCount) {
		return findByProperty(RESOURCE_TYPE, CResourceType,
				rowStartIdxAndCount);
	}
	
	public static ResourceDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (ResourceDAO) ctx.getBean("ResourceDAO");
	}

	public List<Resource> findResourceByRoleId(Integer roleId) {
		String sql = " select model from Resource model where model.CResourceId in " +
				"( select roleResource.resource.CResourceId from RoleResource roleResource where roleResource.role.CRoleId=?0 )";
		return this.findBySql(sql, roleId);
	}
}